/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.teachassistdbservice.util;

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.pinyin4j.PinyinHelper;

/**
 *
 * @author dusin
 */
public class TheUtil {

    //获取当前学期，格式2018-2019-1
    public static String getCurrentSemester() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        return getCurrentSemester(year, month + 1, day);
    }

    public static String getCurrentSemester(int year, int month, int day) {
        //判断是第一学期还是第二学期。 先设定 8月15日至2月15日设置为第一学期，2月16日至8月13日为第二学期

        String semesterStr = "";
        if (month == 9 || month == 10 || month == 11 || month == 12) {
            semesterStr = "" + year + "-" + (year + 1) + "-1";
        } else if (month == 3 || month == 4 || month == 5 || month == 6 || month == 7) {
            semesterStr = "" + (year - 1) + "-" + year + "-2";
        } else if (month == 1) {
            semesterStr = "" + (year - 1) + "-" + (year) + "-1";
        } else if (month == 2) {
            if (day >= 15) {
                semesterStr = "" + (year - 1) + "-" + year + "-2";
            } else {
                semesterStr = "" + (year - 1) + "-" + (year) + "-1";
            }
        } else if (month == 8) {
            if (day >= 15) {
                semesterStr = "" + (year - 1) + "-" + year + "-2";
            } else {
                semesterStr = "" + (year) + "-" + (year + 1) + "-1";
            }
        }
        return semesterStr;
    }
    public static String getJsonResult(boolean success,String msg){
        Gson gson = new Gson();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("status", success);
        map.put("msg", msg);
        return gson.toJson(map);        
    }
    public static String getJSON(List<Map<String, Object>> list, int type) {
        List<Map<String, Object>> targetList = new ArrayList<Map<String, Object>>();
        String typeStr="";
        for (Map<String, Object> m : list) {
            Map<String, Object> tm = new HashMap<String, Object>();
            Map<String, Object> attributes = new HashMap<String, Object>();
            String coursename = (String) m.get("coursename");
            String idStr = m.get("ID").toString();
            
            tm.put("id", m.get("teachclassno") + "|" + idStr+"|"+type);
            tm.put("state", "open");
            StringBuilder sb = new StringBuilder();
            switch (type) {
                case 0:
                    attributes.put("url", "../roster/roster.jsp");
                    attributes.put("type", 0);
                    typeStr="名单";
                    break;
                case 1:
                    attributes.put("url", "../lab/lab.jsp");
                    attributes.put("type", 1);
                    typeStr="实验";
                    break;
                default:
            }

            attributes.put("id", idStr);
            tm.put("attributes", attributes);
            tm.put("text", typeStr+coursename);
            targetList.add(tm);
        }
        Gson gson = new Gson();
        String result = gson.toJson(targetList);
        return result;
    }

    public static String name2Pinyin(String name) {
        StringBuilder sb = new StringBuilder();
        String[] pinyinArray = null;
        for (int i = 0; i < name.length(); i++) {
            char s = name.charAt(i);
            pinyinArray = PinyinHelper.toHanyuPinyinStringArray(s);
            if (pinyinArray != null && pinyinArray.length > 0 && pinyinArray[0] != null) {
                sb.append(pinyinArray[0]);
            }
        }
        return sb.toString();
    }
    
    //截取文件后缀名，扩展名
    public static String fileSuffix(String filename){        
        return filename.substring(filename.lastIndexOf(".") + 1);
    }
}
